<?php
class ModelGallery
{
	public function addGallery($filename)
	{
		$sql1 = "SELECT COUNT(gallery_id) AS IMGCOUNT
				 FROM tbl_gallery
				 WHERE filename = '" . $filename . "' ";
		
		$rs = DB::fetchRow($sql1);
		$rw_count = $rs['IMGCOUNT'];
		if($rw_count == 0)
		{
			$sql2 = "INSERT INTO tbl_gallery(filename, status)
					VALUES('" . $filename . "', 1)";
			$add_gallery = DB::exec($sql2);
			if($add_gallery)
			{
				echo "success";
			}
		} else {
			echo "error";
		}
	}
	
	public function getGalleryImages()
	{
		$sql = "SELECT *
				FROM tbl_gallery";
		
		$gallery_arr = DB::fetchAll($sql);
		
		return $gallery_arr;
	}
	
	public function delGalleryImage($gallery_id)
	{
		$sql1 = "SELECT filename
				 FROM tbl_gallery
				 WHERE gallery_id = " . $gallery_id;
		
		$rs = DB::fetchRow($sql1);
		
		$del_thumb = unlink("../images/banners/thumbs/" . $rs['filename']);
		$del_large = unlink("../images/banners/" . $rs['filename']);
		if($del_large)
		{
			$sql2 = "DELETE FROM tbl_gallery
					 WHERE gallery_id = " . $gallery_id;
			//echo $sql2;
			$del_img = DB::exec($sql2);
			if($del_img)
			{
				echo "Selected image has been delete from the gallery";
			}
		}
	}

        function updateimageurl($request)
        {
            $sql="update tbl_gallery set url='".$request["url"]."' where gallery_id=".$request["gallery_id"]."";
           // echo $sql;
            $update=DB::exec($sql);
            if($update){
                echo "<span class='success'>URL update successfully</span>";
            }
        }
}
?>